/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import model.Persoon;
import persistence.PersoonService;
import persistence.VoorstellingService;

/**
 *
 * @author Matthy & Jelle
 */
public class ZoekVoorstellingPanel extends JPanel{
  /**
   * Labels.
   */
  private JLabel persoonLabel;
  private JLabel beginLabel;
  private JLabel eindLabel;
  private JLabel resultaatLabel;

  /**
   * Input fields.
   */
  private JComboBox persoonSelect;
  private JTextField beginField;
  private JTextField eindField;
  private JList resultaatList;

  /**
   * Buttons.
   */
  private JButton submitButton;

  public ZoekVoorstellingPanel() {
    super();
    initialiseerComponenten();
    layoutComponenten();
    initialiseerEventHandlers();
  }

  private void initialiseerComponenten() {
    // Persoon
    this.persoonLabel = new JLabel("Persoon");
    this.persoonSelect = new JComboBox(new PersoonService().getPersonen().toArray());

    // Begin
    this.beginLabel = new JLabel("Begin (dd/MM/yyyy HH:mm:ss)*");
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    this.beginField = new JTextField(format.format(new Date()));

    // Eind
    this.eindLabel = new JLabel("Einde (dd/MM/yyyy HH:mm:ss)*");
    this.eindField = new JTextField(format.format(new Date()));

    // Resultaat
    this.resultaatLabel = new JLabel("Restultaat");
    this.resultaatList = new JList();
    this.resultaatList.setEnabled(false);

    // Button
    this.submitButton = new JButton("Zoek");

  }

  private void layoutComponenten() {
    this.setLayout(new GridLayout(5, 2));

    // Persoon
    this.add(this.persoonLabel);
    this.add(this.persoonSelect);

    // Begin
    this.add(this.beginLabel);
    this.add(this.beginField);

    // Einde
    this.add(this.eindLabel);
    this.add(this.eindField);

    // Resultaat
    this.add(this.resultaatLabel);
    JScrollPane scrollpane = new JScrollPane(this.resultaatList);
    scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollpane.setPreferredSize(new Dimension(scrollpane.getWidth(), 50));
    this.add(scrollpane);

    // Submit
    this.add(this.submitButton);
  }

  private void initialiseerEventHandlers() {
    this.submitButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent ae) {
        Boolean error = false;

        if (persoonSelect.getSelectedIndex() == -1) {
          persoonLabel.setForeground(Color.RED);
          error = true;
        }
        else {
          persoonLabel.setForeground(Color.BLACK);
        }

        Date begin = new Date();
        if (!beginField.getText().isEmpty()) {
          try {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            begin = format.parse(beginField.getText());
            beginLabel.setForeground(Color.BLACK);
          } catch (ParseException ex) {
            error = true;
            beginLabel.setForeground(Color.RED);
          }
        }
        else {
          error = true;
          beginLabel.setForeground(Color.RED);
        }

        Date eind = new Date();
        if (!eindField.getText().isEmpty()) {
          try {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            eind = format.parse(eindField.getText());
            eindLabel.setForeground(Color.BLACK);
          } catch (ParseException ex) {
            error = true;
            eindLabel.setForeground(Color.RED);
          }
        }
        else {
          error = true;
            eindLabel.setForeground(Color.RED);
        }

        if (!error) {
          Persoon persoon = (Persoon)persoonSelect.getSelectedItem();
          Object voorstellingen[] = new VoorstellingService().zoekVoorstelling(begin, eind, persoon).toArray();
          resultaatList.setListData(voorstellingen);
        }
      }
    });
  }



}
